//
//  PictureDetailViewController.m
//  Product_NEWS
//
//  Created by dllo on 15/12/28.
//  Copyright © 2015年 FCH. All rights reserved.
//

#import "PictureDetailViewController.h"
#import "PictureViewCell.h"
#import "PicCommentModel.h"
#import "BigImageViewController.h"
#import "PictureCommentViewCell.h"
@interface PictureDetailViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,retain)UITableView *myTableView;
@property(nonatomic,retain)NSMutableArray *allComArray;
@property(nonatomic,assign)BOOL isUpLoading;
@property(nonatomic,assign)NSInteger page;
@end

@implementation PictureDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.allComArray = [NSMutableArray array];
    [self initUI];
    [self registCell];
    [self addHeader];
    [self addFooter];
   
     self.navigationController.navigationBar.barTintColor = [UIColor colorWithWhite:0.956 alpha:1.000];
}

-(void)registCell
{
    [self.myTableView registerClass:[PictureViewCell class] forCellReuseIdentifier:@"Piccell"];
     [self.myTableView registerClass:[PictureCommentViewCell class] forCellReuseIdentifier:@"PicCommonCell"];
}


-(void)initUI
{
     self.navigationItem.title = @"评论";
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(10 *SCREEN_WIDTH, 0, 355 * SCREEN_WIDTH, 657 * SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    self.myTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.myTableView.backgroundColor = [UIColor colorWithWhite:0.929 alpha:1.000];
    [_myTableView release];
}

-(void)addHeader
{
    __block PictureDetailViewController *vc = self;
    [self.myTableView addHeaderWithCallback:^{
        vc.page = 1;
        vc.isUpLoading = NO;
        [vc GetPersonList:vc.page];
        
    }];
    [self.myTableView headerBeginRefreshing];

}

-(void)addFooter
{
    __block PictureDetailViewController *vc = self;
    [self.myTableView addFooterWithCallback:^{
        
        vc.page ++;
        vc.isUpLoading = YES;
        [vc GetPersonList:vc.page];
    }];
}
-(void)GetPersonList:(NSInteger)nextCursor
{
    
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    
    NSString *getstr = [NSString stringWithFormat:@"http://api.budejie.com/api/api_open.php?a=dataList&appname=baisibudejie&asid=64FD4D2D-91F4-4619-8089-29D43611F42B&c=comment&client=iphone&data_id=%@&hot=1&jbk=1&mac=&market=&page=%ld&per=20", self.model.id, self.page];
    
    [mgr GET:getstr parameters:dic success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *allDic = responseObject;
        NSArray *arr = responseObject;
        if (arr.count == 0) {
            [self.myTableView headerEndRefreshing]; // 停止
            [self.myTableView footerEndRefreshing];
            return;
        }
        NSArray *array = [allDic objectForKey:@"data"];
            if (self.page == 1) {
                self.allComArray = [NSMutableArray array];
            }
            for (NSDictionary *dic in array) {
                PicCommentModel *picCommentModel = [[PicCommentModel alloc] init];
                NSDictionary *userDic = [dic objectForKey:@"user"];
                [picCommentModel setValuesForKeysWithDictionary:userDic];
                picCommentModel.content = [dic objectForKey:@"content"];
                
                
                [self.allComArray addObject:picCommentModel];
            }
            
            [self.myTableView reloadData];
        
        
        [self.myTableView headerEndRefreshing]; // 停止
        [self.myTableView footerEndRefreshing];    } failure:^(AFHTTPRequestOperation * _Nonnull operation, NSError * _Nonnull error) {
        MBProgressHUD *mb = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        mb.mode = MBProgressHUDModeText;
        mb.labelText = @"网络不给力";
        [mb hide:YES afterDelay:2];
    }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
//行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }else{
        return self.allComArray.count;
    }
}


//表头
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"评论: ";
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        return 25;
    }
    return 0;
}

// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        CGFloat picWidth = self.view.frame.size.width - 20; // 保证宽度一致
        CGFloat scale = picWidth / self.model.width; // 缩放比例
        CGFloat picHeight = (CGFloat)self.model.height * scale; // 保证比例不失调
        if (picHeight > 667.0) {
            picHeight = 300;
        }
        return picHeight + [PictureViewCell heightForText:self.model.text width:self.view.frame.size.width - 40] + 60 + 20;
        
    }else if (self.allComArray.count != 0){
        
        PicCommentModel *picComModel = [self.allComArray objectAtIndex:indexPath.row];
        CGFloat hight = [PictureCommentViewCell heightForText:picComModel.content width:self.view.frame.size.width - 100];
        if (hight > 40) {
            return 30 + hight + 5;
        }else{
            return 50 + 20;
        }
    }else{
        return 0;
    }
    
}




- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    PictureViewCell *picCell = [tableView cellForRowAtIndexPath:indexPath];
    PictureCommentViewCell *comCell = [tableView dequeueReusableCellWithIdentifier:@"PicCommonCell"];
    if (0 == indexPath.section) {
        if (picCell == nil) {
            picCell = [[PictureViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Piccell"];
        }
        picCell.model = self.model;
        
        // 添加手势 点击图片查看大图
        UITapGestureRecognizer *bigTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        picCell.picImageView.userInteractionEnabled = YES;
        picCell.picImageView.tag = indexPath.row;
        [picCell.picImageView addGestureRecognizer:bigTap];
        
        return picCell;
    }else{
        if (comCell == nil) {
            comCell = [[PictureCommentViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"PicCommonCell"];
        }
        comCell.picComModel = self.allComArray[indexPath.row];
        
        return comCell;
    }
    
}


- (void)tapAction:(UITapGestureRecognizer *)tap
{
    // 模态出查看图片页
    //    NSLog(@"tap 手势");
    BigImageViewController *bigImageVC = [[BigImageViewController alloc] init];
    bigImageVC.model = self.model;
    //    NSLog(@"tap.tag = %ld", tap.view.tag);
    [self.navigationController pushViewController:bigImageVC animated:YES];
    
    self.navigationController.navigationBarHidden = YES;
    
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
